package org.ceteca.explica.client.warehouse;

import org.apache.log4j.Logger;
import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.util.BarcodeGenerator;
import org.ceteca.explica.core.warehouse.InterfaceWarehouse;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.ceteca.javaglade.toolkit.swt.SwtDialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class ProductClientListener extends GenericHandler {
	private Logger logger;
	private ProductClient prodClient;
	
	public ProductClientListener(Logger logger, ProductClient prodClient) {
		super();
		this.logger = logger;
		this.prodClient = prodClient;
//		this.gui = prodClient.getEditGui();
		ClientContext.getInstance().getLogger().debug("GREETINGS ... " + this);
		System.out.println("GREEETINGS .............." + this);
	}

	public void on_productBarcode_changed(Event e) {
		// Get the product barcode text entry
		Text textBarcode = (Text)this.gui.getWidget(InterfaceWarehouse.EDIT_PRODUCT_BARCODE_WIDGETNAME);
		
		// Get the product barcode image entry
		Label labelBarcode = (Label)this.gui.getWidget(InterfaceWarehouse.EDIT_PRODUCT_BARCODE_IMAGE_WIDGETNAME);
		
		// Create the image corresponding to the barcode typed by the user
		ClientContext.getInstance().getLogger().debug("Generating barcode image: " + textBarcode + " --> " + ExplicaGUIBuilder.getInstance().getDisplay());
		Image imgBarcode = null;
		if (textBarcode != null && textBarcode.getText() != null && textBarcode.getText().length() > 0)
			imgBarcode = BarcodeGenerator.createBarcodeImage(textBarcode.getText(), ExplicaGUIBuilder.getInstance().getDisplay());
		else
			imgBarcode = new Image(ExplicaGUIBuilder.getInstance().getDisplay(), 300, 300);
		
		// Update the product barcode image
		labelBarcode.setImage(imgBarcode);
		labelBarcode.setSize(labelBarcode.computeSize(imgBarcode.getBounds().width, imgBarcode.getBounds().height));
		
		// Relayout the product edition dialog
		SwtDialog dialogProduct = this.gui.getDialog(InterfaceWarehouse.EDIT_PRODUCT_DIALOG_NAME);
		dialogProduct.getShell().layout(true);
	}
	
	public void on_product_resized(Event e) {
		this.prodClient.showSelectedProductImage();
	}
	
	

	
	public void on_productImageFirst_clicked(Event e) {
		// Load the first image
		this.prodClient.showFirstProductImage();
		
		// Relayout the product edition dialog
		SwtDialog dialogProduct = this.gui.getDialog(InterfaceWarehouse.EDIT_PRODUCT_DIALOG_NAME);
		dialogProduct.getShell().layout(true);
	}
	
	public void on_productImageLast_clicked(Event e) {
		// Load the last image
		this.prodClient.showLastProductImage();
		
		// Relayout the product edition dialog
		SwtDialog dialogProduct = this.gui.getDialog(InterfaceWarehouse.EDIT_PRODUCT_DIALOG_NAME);
		dialogProduct.getShell().layout(true);
	}
	
	public void on_productImagePrevious_clicked(Event e) {
		// Load the previous image
		this.prodClient.showPreviousProductImage();
		
		// Relayout the product edition dialog
		SwtDialog dialogProduct = this.gui.getDialog(InterfaceWarehouse.EDIT_PRODUCT_DIALOG_NAME);
		dialogProduct.getShell().layout(true);
	}
	
	public void on_productImageNext_clicked(Event e) {
		// Load the next image
		this.prodClient.showNextProductImage();
		
		// Relayout the product edition dialog
		SwtDialog dialogProduct = this.gui.getDialog(InterfaceWarehouse.EDIT_PRODUCT_DIALOG_NAME);
		dialogProduct.getShell().layout(true);
	}
	
	public void on_productImageAdd_clicked(Event e) {
		ClientContext.getInstance().getLogger().debug("Añadiendo imagen .... " + this);
		System.out.println("AÑADIENDO .... " + this);
		// Add a new image
		this.prodClient.addProductImage();
		
		// Relayout the product edition dialog
		SwtDialog dialogProduct = this.gui.getDialog(InterfaceWarehouse.EDIT_PRODUCT_DIALOG_NAME);
		dialogProduct.getShell().layout(true);
	}
	
	public void on_productImageDelete_clicked(Event e) {
		// Delete the selected image
		this.prodClient.delProductImage();
		
		// Relayout the product edition dialog
		SwtDialog dialogProduct = this.gui.getDialog(InterfaceWarehouse.EDIT_PRODUCT_DIALOG_NAME);
		dialogProduct.getShell().layout(true);
	}
}
